package cn.itcast.bos.web.action.base;

import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

import com.opensymphony.xwork2.ActionContext;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea>{
	
	@Autowired
	private FixedAreaService fixedAreaService;
	//添加定区
	@Action(value="fixed_area_save",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String save() {
		fixedAreaService.save(model);
		return SUCCESS;
	}
	
	//查询定区
	@Action(value="fixed_Area_pageQuery",results={@Result(name="success",type="json")})
	public String fixedArea_pageQuery() {
		Pageable pageable=new PageRequest(page-1, rows);
		//进行判断
		Specification<FixedArea> specification=new Specification<FixedArea>() {
			
			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate=cb.conjunction();
				//定区编码String id
				if (StringUtils.isNotBlank(model.getId())) {
					predicate=cb.and(predicate,cb.equal(root.get("id").as(String.class), model.getId()));
				}
				//所属单位String company
				if (StringUtils.isNotBlank(model.getCompany())) {
					predicate=cb.and(predicate,cb.like(root.get("company").as(String.class), "%"+model.getCompany()+"%"));
				}
				
				//分区
				return predicate;
			}
		};
		
		Page<FixedArea> page=fixedAreaService.fixedArea_pageQuery(specification,pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	
	//区域的逻辑性删除
	@Action(value="fixed_area_updateTag",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String updataTag() {
		fixedAreaService.updateTag(tag,ids);
		return SUCCESS;
	}
	
	//查询所有没有关联定区的客户
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(name="success",type="json")})
	public String findNoAssociationCustomers() {
		
		//accept是接收传送回来的数据的格式
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
		.accept(MediaType.APPLICATION_JSON)
		.getCollection(Customer.class);
		
		//压入栈顶
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	//查询已经关联定区的的客户信息
	
	@Action(value="fixedArea_findHasAssociationFixedAreaCustomers",results={@Result(name="success",type="json")})
	public String findHasAssociationFixedAreaCustomers() {
		//accept是接收传送回来的数据的格式
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"+model.getId())
		.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
		.getCollection(Customer.class);
		
		//压入栈顶
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	//属性驱动CustomerIds只接收在关联框里的数据
	private String[] customerIds;
	
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	//关联定区用户
	@Action(value="fixedArea_associationCustomersToFixedArea",results={@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")})
	public String associationCustomersToFixedArea() {
		String customerIdStr = StringUtils.join(customerIds,",");
		WebClient.create("http://localhost:9002/crm_management/services/customerService/associationcustomerstofixedarea?customerIdStr="+customerIdStr+"&fixedAreaId="+model.getId()).put(null);
		return SUCCESS;
	}
	
	//属性驱动自己接收参数
	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	//关联快递员
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",location="./pages/base/fixed_area.html",type="redirect")})
	public String associationCourierToFixedArea() {
		//定区id，已经自动封装到model
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		
		return SUCCESS;
	}
	
	//查询所有定区
	@Action(value="fixedArea_findAll",results={@Result(name="success",type="json")})
	public String  findAllFixedArea() {
		List<FixedArea> fixedAreas=fixedAreaService.findAllFixedArea();
		ActionContext.getContext().getValueStack().push(fixedAreas);
		return SUCCESS;
	}
}
